﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AUV5.Web.Pages;
using AUV5.Model;

namespace AUV5.Web.Handler
{
    /// <summary>
    /// Summary description for ShareHandler
    /// </summary>
    public class ShareHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            int pagesize = 0;
            int pageindex = 0;
            bool iscover = false;
            int maxcount = 0;
            pagesize = int.Parse(context.Request["pagesize"]);
            pageindex = int.Parse(context.Request["pageindex"]);
            iscover = bool.Parse(context.Request["iscover"] ?? "false");
            maxcount = int.Parse(context.Request["maxcount"] ?? "0");


            ViewManager<UCWorkShare> viewManager = new ViewManager<UCWorkShare>();


            int recordcount = 0;

            IList<string> viewlist = new List<string>();
            IList<WorksShare> list = BLL.WorksShareBLL.Instance.GetPagedShare(pagesize, pageindex, ref recordcount);
            foreach (var item in list)
            {
                UCWorkShare share = viewManager.LoadViewControl("/Pages/UCWorkShare.ascx");
                share.DataSource = item;
                share.IsCover = iscover;
                viewlist.Add(viewManager.RenderView(share));
            }

            //如果有返回最大记录数要求（maxcount!=0）,不足则补空白的Share
            if (maxcount != 0)
            {
                for (int i = maxcount - viewlist.Count; i > 0; i--)
                {
                    UCWorkShare emptyshare = viewManager.LoadViewControl("/Pages/UCWorkShare.ascx");
                    viewlist.Add(viewManager.RenderView(emptyshare));
                }
            }


            context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(viewlist));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}